/**
 * Created by Administrator on 2017/5/20.
 * @name:   vip-admin 后台模板 菜单navJS
 * @author: 随丶
 * 
 * HuHaiqin 更新内容:
 * 1.支持icon为空的情况；
 * 2.支持a标签添加其他属性，配置的json里面多一个param的参数
 * 3.支持通过配置json来载入其他的main-nav菜单
 */
layui.define(['layer', 'element'], function (exports) {
    // 操作对象
    var layer = layui.layer
        , element = layui.element
        , $ = layui.jquery;
    
     
    // 封装方法
    var mod = {
        // 添加 HTMl
        addHtml: function (addr, obj, treeStatus, data) {
            // 请求数据
            $.post(addr, data, function (res) {
                var view = "";
                if (res.data) {
                    $(res.data).each(function (k, v) {
                        v.subset && treeStatus ? view += '<li class="layui-nav-item layui-nav-itemed">' : view += '<li class="layui-nav-item">';
                        var param = "";
                        if(typeof(v.param) != "undefined"){
                        	param = v.param;
                        }
                        if (v.subset) {
                        	if(v.icon == ""){
                        		view += '<a href="javascript:;" '+param+'>' + v.text + '</a><dl class="layui-nav-child">';
                                $(v.subset).each(function (ko, vo) {
                                	var tmpParam = "";
                                	if(typeof(vo.param) != "undefined"){
                                		tmpParam = vo.param;
                                	}
                                    view += '<dd><a href="javascript:;" '+tmpParam+' href-url="' + vo.href + '">' + vo.text + '</a></dd>';
                                });
                                view += '<dl>';
                        	}else{
                        		view += '<a href="javascript:;" '+param+'><i class="layui-icon">' + v.icon + '</i>' + v.text + '</a><dl class="layui-nav-child">';
                                $(v.subset).each(function (ko, vo) {
                                	var tmpParam = "";
                                	if(typeof(vo.param) != "undefined"){
                                		tmpParam = vo.param;
                                	}
                                    view += '<dd><a href="javascript:;" '+tmpParam+' href-url="' + vo.href + '"><i class="layui-icon">' + vo.icon + '</i>' + vo.text + '</a></dd>';
                                });
                                view += '<dl>';
                        	}
                            
                        } else {
                            if (v.target) {
                                view += '<a href="' + v.href + '" '+param+' target="_blank">';
                            } else {
                                view += '<a href="javascript:;" '+param+' href-url="' + v.href + '">';
                            }
                            if(v.icon == ""){
                                view += '' + v.text + '</a>';
                            }else{
                                view += '<i class="layui-icon">' + v.icon + '</i>' + v.text + '</a>';
                            }
                        }
                        view += '</li>';
                    });
                } else {
                    layer.msg('接受的菜单数据不符合规范,无法解析');
                }
                // 添加到 HTML
                $(document).find(".layui-nav[lay-filter=" + obj + "]").html(view);
                // 更新渲染
                element.init();
                //需要重新监听一下
        	    listen_reload_main_menu();
            });
        }
        // 左侧主体菜单 [请求地址,过滤ID,是否展开,携带参数]
        , main: function (addr, obj, treeStatus, data) {
            // 添加HTML
            this.addHtml(addr, obj, treeStatus, data);
        }
        // 顶部左侧菜单 [请求地址,过滤ID,是否展开,携带参数]
        , top_left: function (addr, obj, treeStatus, data) {
            // 添加HTML
            this.addHtml(addr, obj, treeStatus, data);
        }
        ,curMenuKey:'' //当前选中的菜单，防止重置点击，浪费资源
        /*// 顶部右侧菜单
         ,top_right: function(){

         }*/
    };

    
    //监听载入其他的main-menu
    listen_reload_main_menu();
    function listen_reload_main_menu(){
    	//需要先解绑click在绑定click，防止重复绑定
    	
        $('.reload-main-menu').unbind("click").click(function(){
    		var menuKey = $(this).attr('data-menu-key');
    		if(mod.curMenuKey != menuKey){
    			mod.curMenuKey = menuKey;
    			var url = '/tools/json/getjson?key='+menuKey;
        		mod.main(url,'side-main',true);
    		}
    		
    	});
    	
    }
    
    // 输出
    exports('vip_nav', mod);
});


